package com.wgts.wgts.mvp.view.activity.goodsinfo;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.dialog.AbstractCustomDialogFragment;
import com.wgts.wgts.control.dialog.ToastDate;
import com.wgts.wgts.control.dialog.ToastDialogFragment;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.localbean.LocalProductInfo;
import com.wgts.wgts.thirdParty.share.ShareTools;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.MoHuGlide;
import com.wgts.wgts.tools.QRCodeUtil;
import com.wgts.wgts.tools.Tools;
import com.hjq.permissions.XXPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class ShareDialogFragment extends AbstractCustomDialogFragment {


    private static final String PRODUCT_INFO = "product_info";


    Unbinder unbinder;
    @BindView(R.id.img_product)
    ImageView imgProduct;
    @BindView(R.id.tv_product_name)
    TextView tvProductName;
    @BindView(R.id.tv_product_price)
    TextView tvProductPrice;
    @BindView(R.id.tv_sell)
    TextView tvSell;
    @BindView(R.id.img_code)
    ImageView imgCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.linear_save_view)
    LinearLayout linearSaveView;
    @BindView(R.id.linear_share_wx)
    LinearLayout linearShareWx;
    @BindView(R.id.linear_share_wxfriend)
    LinearLayout linearShareWxfriend;
    @BindView(R.id.linear_save)
    LinearLayout linearSave;
    @BindView(R.id.img_close)
    ImageView imgClose;
    @BindView(R.id.linear_close)
    LinearLayout linearClose;

    private LocalProductInfo productInfo;

    private String shareId;

    public static final String SHAREID = "shareId";

    String sharePath;

    @Override
    public View onCreateDialogView() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.share_goods_dialog, null);
        unbinder = ButterKnife.bind(this, view);
        init();
        initView();
        return view;
    }

    private void init() {
        productInfo = getArguments().getParcelable(PRODUCT_INFO);
        shareId = getArguments().getString(shareId);
    }

    private void initView() {
        MoHuGlide.getInstance().raduice(getContext(), productInfo.productSmallImage, imgProduct);
        tvSell.setText("已售" + productInfo.sellCount + "件");
        tvProductPrice.setText("￥" + productInfo.retailPrice);
        tvProductName.setText(productInfo.productName);

        sharePath = Tools.shareCodePath(productInfo.productUrl, "product", String.valueOf(productInfo.productId));

        imgCode.setImageBitmap(QRCodeUtil.createQRCodeBitmap(sharePath, 70, 70));

        if(AppInfor.getInstance().getInviteCode() != null && !AppInfor.getInstance().getInviteCode().equals("")){
            tvCode.setVisibility(View.VISIBLE);
            tvCode.setText("邀请码:" + AppInfor.getInstance().getInviteCode());
        }else{
            tvCode.setVisibility(View.INVISIBLE);
        }
    }

    public static ShareDialogFragment getInstance(LocalProductInfo productInfo, String shareId) {
        ShareDialogFragment fragment = new ShareDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(PRODUCT_INFO, productInfo);
        bundle.putString(SHAREID, shareId);
        fragment.setArguments(bundle);
        return fragment;
    }






    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.linear_share_wx, R.id.linear_share_wxfriend, R.id.linear_save, R.id.linear_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.linear_share_wx:
                ShareTools.getInstance().startShare(getActivity(), sharePath, productInfo.productSmallImage, "【微广自营】" + productInfo.productName, productInfo.productSubtitle, 0);
                break;
            case R.id.linear_share_wxfriend:
                ShareTools.getInstance().startShare(getActivity(), sharePath, productInfo.productSmallImage, "【微广自营】" + productInfo.productName, productInfo.productSubtitle, 1);
                break;
            case R.id.linear_save:
                clickLinearSave();
                break;
            case R.id.linear_close:
                dismiss();;
                break;
            case R.id.img_close:
                dismiss();
                break;
        }
    }

    private void clickLinearSave() {
        ShareDialogFragmentPermissionsDispatcher.saveShareImgWithPermissionCheck(this);
    }

    @NeedsPermission({Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE})
    public void saveShareImg(){
        Tools.KeepBitmap(linearSaveView, getContext());
    }

    @OnPermissionDenied({Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE})
    public void permissionDenied(){
        MoHuToast.showToast("您已拒绝了相应权限，保存图片失败");
    }

    @OnNeverAskAgain({Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE})
    public void askAgain(){
        ToastDate toastDate = new ToastDate();
        toastDate.setCancelQuestion("拒绝权限部分功能将不能使用，请去设置打开权限");
        toastDate.setCancelText("去设置");
        toastDate.setPointText("取消");
        toastDate.setFragmentManager(getChildFragmentManager());
        ToastDialogFragment fragment = ToastDialogFragment.setToastMessage(toastDate);
        fragment.show(getChildFragmentManager(), "");
        fragment.setOnClickListener(new ToastDialogFragment.OnClickListener() {
            @Override
            public void onClickCancle() {
                MoHuToast.showToast("您已拒绝了相应权限，保存图片失败");
            }

            @Override
            public void onClickEnsuer() {

                XXPermissions.gotoPermissionSettings(getContext());
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        ShareDialogFragmentPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }


}
